package com.example.android_pti_backup;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;

public class DaftarMenu extends Activity {

	ListView lvMakanan;
	ArrayAdapter<String> adapterMakanan;
	EditText inputCariMakanan;
	ArrayList<HashMap<String, String>> makananList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_daftar_menu);
//		String makan[] = { "Nasi Putih", "Nasi Goreng", "Nasi Uduk",
//				"Nasi Liwet", "Nasi Kuning" };
		
		lvMakanan = (ListView) findViewById(R.id.listMakanan);
		inputCariMakanan = (EditText) findViewById(R.id.cariMakanan);

		
		inputCariMakanan.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence cs, int arg1, int arg2,
					int arg3) {
				DaftarMenu.this.adapterMakanan.getFilter().filter(cs);
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
			}

			@Override
			public void afterTextChanged(Editable arg0) {
			}
		});
		
		

		lvMakanan.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> av, View arg1,
					int posisiMakanan, long arg3) {
				String nama_makanan = lvMakanan
						.getItemAtPosition(posisiMakanan).toString();
				Intent intent = new Intent(getApplicationContext(),
						JumlahMenu.class);
				intent.putExtra("nama_makanan", nama_makanan);
				HashMap<String, String> h = new HashMap<String, String>();
				h.put(nama_makanan, "1");
				makananList.add(h);
				
				startActivity(intent);
			}

		});
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				getdatamakanan();
				Log.d("data", makanan[0]);
				runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						adapterMakanan = new ArrayAdapter<String>(DaftarMenu.this, android.R.layout.simple_list_item_1, makanan);
						lvMakanan.setAdapter(adapterMakanan);
					}
				});
			}
		}).start();
		
		
	}
	
	String [] makanan;
	
	public void getdatamakanan(){
		
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet("http://10.0.2.2/selectMenu.php");
		try {
			HttpResponse response = client.execute(get);
			String kategorimakanan = EntityUtils.toString(response.getEntity());
			Log.d("isi", kategorimakanan);
			makanan = kategorimakanan.split(";");
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.kategori_makanan, menu);
		return true;
	}

}
